<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BOOST_TEST: details on expressions</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Boost.Test">
<link rel="up" href="../testing_tools.html" title="Writing unit tests">
<link rel="prev" href="output_stream_testing.html" title="Output streams testing tool">
<link rel="next" href="debugging.html" title="Debugging the assertions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="output_stream_testing.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../testing_tools.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="debugging.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_test.testing_tools.internal_details"></a><a class="link" href="internal_details.html" title="BOOST_TEST: details on expressions"><code class="computeroutput"><span class="identifier">BOOST_TEST</span></code>: details on expressions</a>
</h3></div></div></div>
<p>
        Let's consider the following example:
      </p>
<h6>
<a name="boost_test.testing_tools.internal_details.h0"></a>
        <span class="phrase"><a name="boost_test.testing_tools.internal_details.example_descr"></a></span><a class="link" href="internal_details.html#boost_test.testing_tools.internal_details.example_descr">Example:
        BOOST_TEST reporting</a>
      </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                <p>
                  Code
                </p>
              </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">boost_test_macro3</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test_op_reportings</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">int</span> <span class="identifier">a</span> <span class="special">=</span> <span class="number">13</span><span class="special">,</span> <span class="identifier">b</span> <span class="special">=</span> <span class="number">12</span><span class="special">;</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">a</span> <span class="special">==</span> <span class="identifier">b</span><span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">a</span> <span class="special">&lt;</span> <span class="identifier">b</span><span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">a</span> <span class="special">-</span> <span class="number">1</span> <span class="special">&lt;</span> <span class="identifier">b</span><span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">b</span> <span class="special">&gt;</span> <span class="identifier">a</span> <span class="special">-</span> <span class="number">1</span><span class="special">);</span>
<span class="special">}</span>
</pre>
              </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                <p>
                  Output
                </p>
              </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="special">./</span><span class="identifier">boost_test_macro3</span> <span class="special">--</span><span class="identifier">log_level</span><span class="special">=</span><span class="identifier">all</span>
<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
<span class="identifier">Entering</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"boost_test_macro3"</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">12</span><span class="special">):</span> <span class="identifier">Entering</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"test_op_reportings"</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">15</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_op_reportings"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">a</span> <span class="special">==</span> <span class="identifier">b</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">13</span> <span class="special">!=</span> <span class="number">12</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">16</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_op_reportings"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">a</span> <span class="special">&lt;</span> <span class="identifier">b</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">13</span> <span class="special">&gt;=</span> <span class="number">12</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">17</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_op_reportings"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">a</span> <span class="special">-</span> <span class="number">1</span> <span class="special">&lt;</span> <span class="identifier">b</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">13</span> <span class="special">-</span> <span class="number">1</span> <span class="special">&gt;=</span> <span class="number">12</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">18</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_op_reportings"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">b</span> <span class="special">&gt;</span> <span class="identifier">a</span> <span class="special">-</span> <span class="number">1</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">12</span> <span class="special">&lt;=</span> <span class="number">12</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">12</span><span class="special">):</span> <span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"test_op_reportings"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">484u</span><span class="identifier">s</span>
<span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"boost_test_macro3"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">588u</span><span class="identifier">s</span>

<span class="special">***</span> <span class="number">2</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"boost_test_macro3"</span>
</pre>
              </td></tr></tbody>
</table></div>
<p>
        It was already mentioned that the reporting is not symmetrical (see <a class="link" href="reports.html" title="Reported information">here</a>). An expression is
        constructed from the <code class="computeroutput"><span class="identifier">statement</span></code>
        appearing in the <code class="computeroutput"><span class="identifier">BOOST_TEST</span></code>
        macro. This expression allows evaluation and reporting such as <code class="computeroutput"><span class="string">"13 - 1 &gt;= 12"</span> <span class="identifier">failed</span></code>
        along with a copy of the <code class="computeroutput"><span class="identifier">statement</span></code>,
        which contains more details than <code class="computeroutput"><span class="string">"a - 1
        &lt; b"</span> <span class="identifier">failed</span></code>. In
        details, what happens is the following:
      </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
<p class="simpara">
            a special object, the <code class="computeroutput"><span class="identifier">seed</span></code>
            of the expression, is composed from the left side of <code class="computeroutput"><span class="identifier">statement</span></code>.
            This initial composition has highest precedence over the supported operations.
            The expression below:
          </p>
<pre class="programlisting"><span class="identifier">a</span> <span class="identifier">op1</span> <span class="identifier">b</span> <span class="identifier">op2</span> <span class="identifier">c</span> <span class="identifier">op3</span> <span class="identifier">d</span>
</pre>
<p class="simpara">
            is actually seen as
          </p>
<pre class="programlisting"><span class="special">(</span> <span class="identifier">seed</span> <span class="identifier">a</span> <span class="special">)</span> <span class="identifier">op1</span> <span class="identifier">b</span> <span class="identifier">op2</span> <span class="identifier">c</span> <span class="identifier">op3</span> <span class="identifier">d</span>
</pre>
</li>
<li class="listitem">
<p class="simpara">
            The "<code class="computeroutput"><span class="identifier">seed</span> <span class="identifier">a</span></code>"
            returns an <code class="computeroutput"><span class="identifier">expression</span></code>
            object that keep tracks of the type of <code class="computeroutput"><span class="identifier">a</span></code>.
            This expression has overloads for left-to-right associativity, and the
            operations <code class="computeroutput"><span class="identifier">op1</span></code>, <code class="computeroutput"><span class="identifier">op2</span></code> ... are <span class="emphasis"><em>chained</em></span>
            to the right of this expression object:
          </p>
<pre class="programlisting"><span class="identifier">a</span> <span class="identifier">op1</span> <span class="identifier">b</span>
</pre>
<p class="simpara">
            yields to the pseudo-code
          </p>
<pre class="programlisting"><span class="identifier">expression1</span> <span class="special">=</span> <span class="identifier">create</span><span class="special">-</span><span class="identifier">expression</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span>
<span class="identifier">expression2</span> <span class="special">=</span> <span class="identifier">create</span><span class="special">-</span><span class="identifier">expression</span><span class="special">(</span><span class="identifier">expression1</span><span class="special">,</span> <span class="identifier">op1</span><span class="special">,</span> <span class="identifier">b</span><span class="special">)</span>
</pre>
<p class="simpara">
            <code class="computeroutput"><span class="identifier">expression1</span></code> and <code class="computeroutput"><span class="identifier">expression2</span></code> keep track of their left
            and right operands, and the operation on those operands. The expressions
            keep also track of the result type of the associated sub-expression.
            In the above example, <code class="computeroutput"><span class="identifier">expression1</span></code>
            and <code class="computeroutput"><span class="identifier">expression2</span></code> have
            result type <code class="computeroutput"><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">a</span><span class="special">)</span></code>
            and <code class="computeroutput"><span class="keyword">decltype</span><span class="special">(</span><span class="identifier">a</span> <span class="identifier">op1</span>
            <span class="identifier">b</span><span class="special">)</span></code>
            respectively. The result type allows for chaining sub-expressions.
          </p>
</li>
<li class="listitem">
<p class="simpara">
            The C++ operators precedence rules apply in any case. What is seen by
            the expression is what is reachable with left-to-right composition. Any
            other operation that happens before it reaches the expression's right
            operand is not parsed as a sub-expression and is seen as a single operand:
            the right operand is not developed further by the framework. Let's suppose
            <code class="computeroutput"><span class="identifier">op2</span></code> below has higher
            precedence than <code class="computeroutput"><span class="identifier">op1</span></code>,
            then
          </p>
<pre class="programlisting"><span class="identifier">a</span> <span class="identifier">op1</span> <span class="identifier">b</span> <span class="identifier">op2</span> <span class="identifier">c</span>
</pre>
<p class="simpara">
            is equivalent to:
          </p>
<pre class="programlisting"><span class="identifier">create</span><span class="special">-</span><span class="identifier">expression</span><span class="special">(</span><span class="identifier">create</span><span class="special">-</span><span class="identifier">expression</span><span class="special">(</span><span class="identifier">a</span><span class="special">),</span> <span class="identifier">op1</span><span class="special">,</span> <span class="special">(</span><span class="identifier">b</span> <span class="identifier">op2</span> <span class="identifier">c</span><span class="special">))</span>
</pre>
<p class="simpara">
            In the above statement, the final expression can only see the result
            of <code class="computeroutput"><span class="special">(</span><span class="identifier">b</span>
            <span class="identifier">op2</span> <span class="identifier">c</span><span class="special">)</span></code> to its right, for which no further detail
            can be provided in the logs. This is also the case for <span class="emphasis"><em>right-to-left</em></span>
            associative operators, such as <code class="computeroutput"><span class="special">!</span></code>,
            <code class="computeroutput"><span class="special">~</span></code>, <code class="computeroutput"><span class="special">-</span></code>
            (unary negation) etc.
          </p>
<div class="caution"><table border="0" summary="Caution">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../doc/src/images/caution.png"></td>
<th align="left">Caution</th>
</tr>
<tr><td align="left" valign="top"><p>
              Since the <code class="computeroutput"><span class="identifier">expression</span></code>
              object is composed from left-to-right, it actually observes a chain
              of operations and not the full expression tree.
            </p></td></tr>
</table></div>
</li>
<li class="listitem">
<p class="simpara">
            Once the full expression chain is built, it is evaluated as a chain of
            sub-expressions from left-to-right, exactly as the composition rule above.
            The evaluated elements are the ones of the expression itself. The expression
          </p>
<pre class="programlisting"><span class="identifier">a</span> <span class="identifier">op1</span> <span class="identifier">b</span>
</pre>
<p class="simpara">
            yields to the following evaluation chain:
          </p>
<pre class="programlisting"><span class="identifier">expression2</span><span class="special">.</span><span class="identifier">result</span> <span class="special">=</span> <span class="identifier">expression1</span><span class="special">.</span><span class="identifier">result</span> <span class="identifier">op1</span> <span class="identifier">b</span>
<span class="identifier">expression1</span><span class="special">.</span><span class="identifier">result</span> <span class="special">=</span> <span class="identifier">a</span>
</pre>
<p class="simpara">
            The final expression of the statement is cast to a boolean, which is
            in turn evaluated by the <span class="emphasis"><em>Unit Test Framework</em></span>.
          </p>
</li>
</ol></div>
<p>
        The example below illustrates the construction of the left-to-right <span class="emphasis"><em>chained</em></span>
        expression.
      </p>
<h6>
<a name="boost_test.testing_tools.internal_details.h1"></a>
        <span class="phrase"><a name="boost_test.testing_tools.internal_details.example_descr0"></a></span><a class="link" href="internal_details.html#boost_test.testing_tools.internal_details.example_descr0">Example:
        BOOST_TEST compound statements</a>
      </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                <p>
                  Code
                </p>
              </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">boost_test_macro2</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test_op_precedence</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">int</span> <span class="identifier">a</span> <span class="special">=</span> <span class="number">13</span><span class="special">,</span> <span class="identifier">b</span> <span class="special">=</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">c</span> <span class="special">=</span> <span class="number">12</span><span class="special">;</span>
  <span class="comment">// left term of == is expanded in the logs</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">a</span> <span class="special">%</span> <span class="identifier">b</span> <span class="special">==</span> <span class="identifier">c</span><span class="special">);</span>
  <span class="comment">// right term of == is not expanded in the logs</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">a</span> <span class="special">==</span> <span class="identifier">c</span> <span class="special">%</span> <span class="identifier">b</span><span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test_op_right_associative</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">int</span> <span class="identifier">a</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">a</span><span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(!</span><span class="identifier">a</span><span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(--</span><span class="identifier">a</span><span class="special">);</span>
<span class="special">}</span>
</pre>
              </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                <p>
                  Output
                </p>
              </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="special">./</span><span class="identifier">boost_test_macro2</span> <span class="special">--</span><span class="identifier">log_level</span><span class="special">=</span><span class="identifier">all</span>
<span class="identifier">Running</span> <span class="number">2</span> <span class="identifier">test</span> <span class="identifier">cases</span><span class="special">...</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">16</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_op_precedence"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">a</span> <span class="special">%</span> <span class="identifier">b</span> <span class="special">==</span> <span class="identifier">c</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">13</span> <span class="special">%</span> <span class="number">2</span> <span class="special">!=</span> <span class="number">12</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">18</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_op_precedence"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">a</span> <span class="special">==</span> <span class="identifier">c</span> <span class="special">%</span> <span class="identifier">b</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">13</span> <span class="special">!=</span> <span class="number">0</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">25</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_op_right_associative"</span><span class="special">:</span> <span class="identifier">check</span> <span class="special">!</span><span class="identifier">a</span> <span class="identifier">has</span> <span class="identifier">failed</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">26</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_op_right_associative"</span><span class="special">:</span> <span class="identifier">check</span> <span class="special">--</span><span class="identifier">a</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[(</span><span class="keyword">bool</span><span class="special">)</span><span class="number">0</span> <span class="identifier">is</span> <span class="keyword">false</span><span class="special">]</span>

<span class="special">***</span> <span class="number">4</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"boost_test_macro2"</span>
</pre>
              </td></tr></tbody>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2022 Boost.Test contributors<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="output_stream_testing.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../testing_tools.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="debugging.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
